/**
 * Created by Administrator on 2020/11/24.
 */
layui.use(['table','form','layer','layedit', 'laydate'], function() {
    var table = layui.table
        , form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        , $ = layui.$;

    //监听提交
    form.on('submit(demo1)', function(data){
        table.reload('test',{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where:data.field
        });
        return false;
    });
    $.ajaxSetup({
        xhrFields: {withCredentials: true}
    });
    table.render({
        elem: '#test'
        , url: config.gatewayUri+ '/api/goods/admin/getBackGoodsList'
        //, toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , cols: [[
            {field: 'routeNumber', title: '货柜号',sort: true, templet: function (d) {
                return d.rownumber + "-" + d.columnnumber;
            }}
            , {field: 'deviceName', title: '设备名称'}
            , {field: 'goodsid', title: '商品编号', templet: function (d) {
                if (d.goodsid != null){
                    return d.goodsid;
                }else{
                    return "";
                }
            }}
            , {field: 'goodsname', title: '商品名称', templet: function (d) {
                if (d.goodsid != null){
                    return d.goodsname;
                }else{
                    return "";
                }
                }}
            , {field: 'goodspic', title: '商品图片', templet: function (d) {
                if (d.goodsid != null){
                    return d.goodspic;
                }else{
                    return "";
                }
            }}
            , {field: 'goodsaccpoints', title: '商品积分', templet: function (d) {
                if (d.goodsid != null){
                    return d.goodsaccpoints;
                }else{
                    return "";
                }
            }}
            , {field: 'stockqty', title: '库存数量', templet: function (d) {
                if (d.goodsid != null){
                    return d.stockqty;
                }else{
                    return "";
                }
            }}
            , {field: 'isflag', title: '商品状态', templet: function (d) {
                    if (d.goodsid != null){
                        if (d.isflag == 1) return "上架";
                        if (d.isflag == 2) return "下架";
                    }else{
                        return "";
                    }
                }}
            , {
                field: 'isstock', title: '是否缺货', templet: function (d) {
                    if (d.goodsid != null){
                        if (d.isstock == 1) return "正常";
                        if (d.isstock == 2) return "缺货";
                    }else{
                        return "";
                    }
                }
            }
            , {
                field: 'isfault', title: '是否故障', templet: function (d) {
                    if (d.isfault == 1) return "正常";
                    if (d.isfault == 2) return "故障";
                }
            }
            , {title: '操作', width: 240, templet: '#newsListBar', fixed: "right", align: "center"}
        ]]
        , page: true
        , limits: [5, 10]
        , limit: 5
        , response: {
            statusCode: '200'
        }
        , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        }
    });

    //检查项目添加到下拉框中
    $('.isStock').append(new Option("正常", 1));
    $('.isStock').append(new Option("缺货", 2));
    $('.isFault').append(new Option("正常", 1));
    $('.isFault').append(new Option("故障", 2));
    layui.form.render("select");

    table.on('tool(newsList)', function(obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'bh') { //补货
            layer.open({
                //调整弹框的大小
                area:['500px','200px'],
                shadeClose:true,//点击旁边地方自动关闭
                //动画
                anim:2,
                //弹出层的基本类型
                type: 2,
                //刚才定义的弹窗页面
                content:['pages/goods/goods_bh.html', 'no'],
                success: function(layero,index){
                    var body = layer.getChildFrame('body', index);
                    body.find("#cabinetid").val(data.cabinetid);
                    form.render();
                }
            });
        }else if(layEvent==='bj'){//编辑
            layer.open({
                //调整弹框的大小
                area:['500px','350px'],
                shadeClose:true,//点击旁边地方自动关闭
                //动画
                anim:2,
                //弹出层的基本类型
                type: 2,
                //刚才定义的弹窗页面
                content:['pages/goods/goods_bj.html', 'no'],
                success: function(layero,index){
                    var body = layer.getChildFrame('body', index);
                    body.find("#cabinetid").val(data.cabinetid);
                    body.find("#goodscategoryids").val(data.goodscategoryid);
                    body.find("#goodsids").val(data.goodsid);
                    form.render();
                }
            });
        }else if(layEvent==='sj'){//上架
            layer.confirm("确定要上架"+data.goodsname+"吗？",function(){
                $.get(config.gatewayUri+"/"+config.goodsServiceId + "/api/goods/admin/updateIsOff", "isflag=1&cabinetid=" + data.cabinetid, function (data) {
                    if (data.code == 200) {
                        layer.alert("上架成功");
                    } else {
                        layer.alert("上架失败");
                    }
                    table.reload('test', {});
                });
            });
        }else if(layEvent==='add') {//添加商品
            layer.open({
                //调整弹框的大小
                area:['500px','350px'],
                shadeClose:true,//点击旁边地方自动关闭
                //动画
                anim:2,
                //弹出层的基本类型
                type: 2,
                //刚才定义的弹窗页面
                content:['pages/goods/goods_add.html', 'no'],
                success: function(layero,index){
                    var body = layer.getChildFrame('body', index);
                    body.find("#cabinetid").val(data.cabinetid);
                    form.render();
                }
            });
        }else if(layEvent==='xj'){ //下架
            layer.confirm("确定要下架"+data.goodsname+"吗？",function() {
                $.get(config.gatewayUri+"/"+config.goodsServiceId  + "/api/goods/admin/updateIsOff", "isflag=2&cabinetid=" + data.cabinetid, function (data) {
                    if (data.code == 200) {
                        layer.alert("下架成功");
                    } else {
                        layer.alert("下架失败");
                    }
                    table.reload('test', {});
                });
            });
        }
    });
});